<?php

namespace Lettral\Helpers;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\ConstraintViolationList;

class ResponseCreator
{	
	public static function getErrorList($errors) {
    	
		$response = new \Symfony\Component\HttpFoundation\Response();				
		$response->headers->set('Content-Type', 'application/json; charset=UTF-8');			
		$errorList = array();
		
		foreach ($errors->getIterator() as $error) {			
			$errorItem = array();
			$errorItem['id'] = $error->getPropertyPath();
			$errorItem['message'] = $error->getMessage();
			$errorList[] = $errorItem;
		} 

		$response->setContent(json_encode($errorList));
		
		return $response;
	}
	
	public static function getError($errorMsg) {
		$response = new \Symfony\Component\HttpFoundation\Response();
		$response->headers->set('Content-Type', 'application/json; charset=UTF-8');
		$error['status'] = "error";
		$error['message'] = $errorMsg;
		$response->setContent(json_encode($error));
		return $response;
	}
	
	public static function getSuccess($id = null) {
		
		$response = new \Symfony\Component\HttpFoundation\Response();
		$response->headers->set('Content-Type', 'application/json; charset=UTF-8');
		$success['status'] = "success";
		if (isset($id))
			$success['id'] = $id;
		$response->setContent(json_encode($success));
		return $response;
	}
	
	public static function getUrl($url) {
		
		$response = new \Symfony\Component\HttpFoundation\Response();
		$response->headers->set('Content-Type', 'application/json; charset=UTF-8');
		$success['url'] = $url;
		$response->setContent(json_encode($success));
		return $response;
	}
	
	/**
	 * Permet de télécharger une lettre
	 * La vérification des droits doit être faite en amont
	 * @param int $numLettre identifiant de la lettre
	 */
	public static function getPdf($filePath, $fileName) {
		
		$response = new Response();
		$response->setContent(file_get_contents($filePath));
		$response->headers->set('Content-Type', 'application/force-download'); // si le type mime est connu du navigateur, il va tenter de l'afficher et non de le télécharger
		$response->headers->set('Content-disposition', 'filename='. $fileName);
		return $response;
	}
	
	/**
	 * Permet de voir une lettre
	 * La vérification des droits doit être faite en amont
	 * @param int $numLettre identifiant de la lettre
	 */
	public static function showPdf($filePath, $fileName) {
	
		$response = new Response();
		$response->setContent(file_get_contents($filePath));
		$response->headers->set('Content-Type', 'application/pdf');
		$response->headers->set('Content-disposition', 'filename='. $fileName);
		return $response;
	}
}